/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bg.rsa_project.pi_calculator;

import bg.rsa_project.enums.Precision;
import bg.rsa_project.enums.ThreadsCount;
import bg.rsa_project.gui.GUI;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigDecimal;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.Action;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JProgressBar;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.SwingWorker;
import javax.swing.border.Border;

/**
 *
 * @author Teofana Gadjeva
 * @author Nikola Pandurski
 */
public class Controller {

    private static GUI gui;
    private static JComboBox threadsCount;
    private static JComboBox precision;
    private static JButton startButton;
    private static JButton terminateButton;
    private static JTextArea resultPi;
    private static JLabel executionTime;
    private static JFileChooser fileChooser;

    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>


        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                guiInit();
                gui.setVisible(true);
            }
        });
    }

    private static void guiInit() {
        gui = new GUI();
        threadsCount = gui.getCmb_threads_count();
        precision = gui.getCmb_precision();
        startButton = gui.getBtn_start();
        terminateButton = gui.getBtn_terminate();
        resultPi = gui.getTxr_pi_result();
        executionTime = gui.getLbl_exec_time();
        fileChooser = gui.getFileChooser();
        //threads count combobox init
        threadsCount.addItem(ThreadsCount.ONE.getCount());
        threadsCount.addItem(ThreadsCount.TWO.getCount());
        threadsCount.addItem(ThreadsCount.FOUR.getCount());
        threadsCount.addItem(ThreadsCount.EIGHT.getCount());
        threadsCount.addItem(ThreadsCount.SIXTEEN.getCount());
        threadsCount.setSelectedIndex(0);

        //pi precision combobox init
        precision.addItem(Precision.FIFTY.getPrecision());
        precision.addItem(Precision.ONE_HUNDRED.getPrecision());
        precision.addItem(Precision.TWO_HUNDRED.getPrecision());
        precision.addItem(Precision.FIVE_HUNDRED.getPrecision());
        precision.addItem(Precision.ONE_THOUSAND.getPrecision());
        precision.addItem(Precision.FIVE_THOUSAND.getPrecision());
        precision.addItem(Precision.TEN_THOUSAND.getPrecision());
        precision.setSelectedIndex(0);

        startButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                ExecutorService execService = Executors.newCachedThreadPool();
                execService.execute(new Runnable() {

                    @Override
                    public void run() {
                        calculate();
                    }
                });
                execService.shutdown();
            }
        });

        terminateButton.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                System.exit(1);
            }
        });

    }

    static void calculate() {
        int threads = ((Integer) threadsCount.getSelectedItem());
        int prec = ((Integer) precision.getSelectedItem());
        resultPi.setText("Please wait while calculating pi ...");
        BigDecimal pi = PiCalculator.calculatePi(threads, prec, true, false);
        resultPi.setText(pi.toString());
        long execTime = PiCalculator.getTime();
        executionTime.setText("" + execTime);
    }
}
